﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using StarsAdmin.Application.Dict.Dtos;
using StarsAdmin.Application.Dict.Services;
using StarsAdmin.Application.Role.Dtos;
using StarsAdmin.Core.Attributes;
using StarsAdmin.Core.Consts;
using StarsAdmin.Core.Models;
using StarsAdmin.Web.Core.Conttrollers;
using System.ComponentModel.DataAnnotations;

namespace StarsAdmin.WebApi.Conttrollers
{
    /// <summary>
    /// 字典类型管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Order(7)]
    [Authorize(Policy = RoleTypeConst.SystemAdmin)]
    public class DictTypeController : BaseController
    {
        private readonly IDictTypeService _dictTypeService;

        public DictTypeController(IDictTypeService dictTypeService)
        {
            _dictTypeService = dictTypeService;
        }

        /// <summary>
        /// 获得所有
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ResultModel<List<DictTypeDto>>> GetAllAsync([FromQuery] BaseQueryInput input)
        {
            return (await _dictTypeService.GetAllAsync(input)).ResultFormat();
        }

        /// <summary>
        /// 分页获取
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ResultModel<Page<DictTypeDto>>> GetPageAsync([FromQuery] PageQueryInput input)
        {
            return (await _dictTypeService.GetPageAsync(input)).ResultFormat();
        }

        /// <summary>
        /// 根据Id获取数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ResultModel<DictTypeDto>> GetByIdAsync([Required] long id)
        {
            return (await _dictTypeService.GetByIdAsync(id)).ResultFormat("没有找到该条数据", 404);
        }

        /// <summary>
        /// 根据Id集合获取数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ResultModel<List<DictTypeDto>>> GetByIdsAsync([FromQuery] BaseIdsInput input)
        {
            return (await _dictTypeService.GetByIdsAsync(input.Ids)).ResultFormat();
        }

        /// <summary>
        /// 批量导出
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> ExportAsync(BaseIdsInput input)
        {
            var result = await _dictTypeService.ExportAsync(input.Ids);
            return result != null ? result : NotFound();
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultModel<bool>> AddAsync(AddDictTypeInput input)
        {
            return (await _dictTypeService.AddAsync(input)).ResultFormat("添加失败");
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ResultModel<bool>> UpdateAsync(UpdateDictTypeInput input)
        {
            return (await _dictTypeService.UpdateAsync(input)).ResultFormat("更新失败");
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<ResultModel<bool>> DeleteAsync([Required] long id)
        {
            return (await _dictTypeService.DeleteAsync(id)).ResultFormat("删除失败");
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<ResultModel<bool>> BatchDeleteAsync(BaseIdsInput input)
        {
            return (await _dictTypeService.BatchDeleteAsync(input.Ids)).ResultFormat("批量删除失败");
        }
    }
}